clc;clear;
% 读取数据
data = readmatrix('last.csv');
freq = data(1:end-1,1);
real = data(1:end-1,2);
imag = data(1:end-1,3);


% 计算群时延
s21 = real + 1j*imag;
phase = unwrap(angle(s21));
group_delay = -diff(phase)./(2*pi*diff(freq));

% 频率对齐
freq_gd = freq(1:end-1) + diff(freq)/2;

% 数据平滑
smoothed_gd = smoothdata(group_delay, 'sgolay', 500);

% 绘图
figure
% plot(freq_gd/1e9, group_delay*1e9, 'Color',[0.5 0.5 0.5], 'LineWidth',0.5)
hold on
plot(freq_gd/1e9, smoothed_gd*1e9, 'b', 'LineWidth',2)
xlabel('Frequency (GHz)')
ylabel('Group Delay (ns)')
title('Filter Group Delay Characteristics')
grid on
legend( 'Smoothed')